package cn.fl.project.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.enums.LogBusinessTypeEnum;
import cn.fl.project.condition.PrjQuatPrpsCfgAiQueryCondition;
import cn.fl.project.service.IPrjQuatPrpsCfgAiService;
import cn.fl.project.vo.PrjQuatPrpsCfgAiVO;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import com.lionbridge.log.ext.ExtSlf4jLogger;
import com.lionbridge.log.ext.ExtSlf4jLoggerBuilder;
import com.lionbridge.log.ext.LogTypeEnum;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 项目报价方案年利率配置表 控制层
 */
@Slf4j
@Controller
@RequestMapping("/project/prjQuatPrpsCfgAi")
public class PrjQuatPrpsCfgAiController extends BaseController{
	private static ExtSlf4jLogger logger = ExtSlf4jLoggerBuilder.create()
			.setLogger(log).setBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setSubBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setLogType(LogTypeEnum.BUSINESS).build().setKeywords("PrjQuatPrpsCfgD");
	/**
	 * PrjQuatPrpsCfgAi 服务层BEAN
	 */
	@Autowired
	private IPrjQuatPrpsCfgAiService prjQuatPrpsCfgAiService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		String prpsCfgId = request.getParameter("prpsCfgId");
		String annIntScale = request.getParameter("annIntScale");//年里率小数位数
		ModelAndView view = new ModelAndView("project/prjQuatPrpsCfgAi/prjQuatPrpsCfgAiAddList");
		logger.info("报价方案年利率配置,跳转到列表页面，prpsCfgId:{},annIntScale:{}", prpsCfgId,annIntScale);
		view.addObject("prpsCfgId", prpsCfgId);
		view.addObject("annIntScale", annIntScale);
		return view;
	}
	
	/**
	 * 到详情列表页面
	 */
	@RequestMapping(value = "/toDetailListPage.do", method = { RequestMethod.GET})
	public ModelAndView toDetailListPage(HttpServletRequest request, HttpServletResponse response) {
		String prpsCfgId = request.getParameter("prpsCfgId");
		ModelAndView view = new ModelAndView("project/prjQuatPrpsCfgAi/prjQuatPrpsCfgAiAddDetailList");
		view.addObject("prpsCfgId", prpsCfgId);
		logger.info("报价方案年利率配置,跳转到详情页面prpsCfgId:{}", prpsCfgId);
		return view;
	}

	/**
	 * 到新增/修改页面
	 */
	@RequestMapping(value = "/toAddPage.do", method = { RequestMethod.GET, RequestMethod.POST })
	public ModelAndView toModifyPage(HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		String prpsCfgId = request.getParameter("prpsCfgId");
		String annIntScale = request.getParameter("annIntScale");//年利率小数位数
		ModelAndView view = new ModelAndView("project/prjQuatPrpsCfgAi/prjQuatPrpsCfgAiAdd");
		//查询下拉框数据
		//租赁期数单位
		List<KeyValueVO> lsPdUntCdList = this.prjQuatPrpsCfgAiService.searchLsPdUntCdList();
		view.addObject("lsPdUntCdList", lsPdUntCdList);
		//是否
		List<KeyValueVO> yesOrNoCdList = this.prjQuatPrpsCfgAiService.searchYesOrNoCdList();
		view.addObject("yesOrNoCdList", yesOrNoCdList);
		
		view.addObject("id", id);
		view.addObject("prpsCfgId", prpsCfgId);
		view.addObject("annIntScale", annIntScale);
		logger.info("报价方案年利率配置,跳转到新增/修改页面prpsCfgId:{},annIntScale:{}", prpsCfgId,annIntScale);
		return view;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
		String currentPage = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		String prpsCfgId = request.getParameter("prpsCfgId");
		Pager page = new Pager();
		if (StringUtils.isNotEmpty(currentPage)) {
			page.setCurrentPage(Integer.parseInt(currentPage));
		}

		if (StringUtils.isNotEmpty(currentPage)) {
			page.setEveryPage(Integer.parseInt(pageSize));
		}

		PrjQuatPrpsCfgAiQueryCondition qc = new PrjQuatPrpsCfgAiQueryCondition();
		if (StringUtils.isNotEmpty(prpsCfgId)) {
			qc.setPrpsCfgId(Long.valueOf(prpsCfgId));
		}
		page.setCondition(qc);

		Pager pager = this.prjQuatPrpsCfgAiService.searchListPage(page);
		List<PrjQuatPrpsCfgAiVO> list = (List<PrjQuatPrpsCfgAiVO>) pager.getResultList();
		ListPageVO<PrjQuatPrpsCfgAiVO> listPage = new ListPageVO<PrjQuatPrpsCfgAiVO>(list, pager.getTotalCount());
		logger.info("报价方案年利率配置,初始化列表result:{}", JSON.toJSONString(page));
		return listPage;
	}

	/**
	 * 删除数据
	 */
	@RequestMapping(value = "/removeById.do", method = {RequestMethod.POST })
	@ResponseBody
	public DataResultVO removeById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String ids = request.getParameter("ids");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_DELETE_SUCC);
		if(StringUtils.isEmpty(ids)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			return result;
		}
		List<Long> idsList = new ArrayList<Long>();
		JSONArray idsArray = JSONArray.fromObject(ids);
		for (Object object : idsArray) {
			idsList.add(Long.valueOf(object.toString()));
		}
		try {
			boolean success = this.prjQuatPrpsCfgAiService.removeByIds(idsList);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_DELETE_FAIL);
				logger.info("报价方案年利率配置,删除数据失败ids:{}",ids);
				return result;
			}
			logger.info("报价方案年利率配置,删除数据ids:{}",ids);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("报价方案年利率配置,删除数据失败ids:{},msg:{}",ids,ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("报价方案年利率配置,删除数据失败ids:{},msg:{}",ids,ex.getMessage());
		}

		return result;
	}

	/**
	 * 得到一条记录
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getById.do", method = { RequestMethod.GET})
	@ResponseBody
	public Object getById(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			logger.info("报价方案年利率配置,根据id获取详情,参数id为空");
			return result;
		}
		
		try {
			PrjQuatPrpsCfgAiVO vo = this.prjQuatPrpsCfgAiService.selectById(Long.valueOf(id));
			if(null == vo){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_QUERY_DATA_IS_EMPTY);
				logger.info("报价方案年利率配置,根据id获取详情,数据不存在id:{}",id);
			}
			result.setData(vo);
			logger.info("报价方案年利率配置,根据id获取详情result:{}", JSON.toJSONString(result));
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("报价方案年利率配置,根据id获取详情,业务异常id:{},msg:{}",id,ex.getMessage());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("报价方案年利率配置,根据id获取详情,系统异常id:{}",id);
		}
		return result;
	}

	/**
	 * 新增
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object add(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_ADD_SUCC);
		PrjQuatPrpsCfgAiVO entity = null;
		try {
			entity = this.getRequestParamPrjQuatPrpsCfgAiAdd(request);
			logger.info("报价方案年利率配置,新增数据entity:{}", JSON.toJSONString(entity));
			Long id = this.prjQuatPrpsCfgAiService.add(entity);
			result.setData(id);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("报价方案年利率配置,新增数据,业务异常msg:{}",ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("报价方案年利率配置,新增数据,系统异常entity：{}", JSON.toJSONString(entity));
		}

		return result;
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/modify.do", method = { RequestMethod.POST })
	@ResponseBody
	public Object modify(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String id = request.getParameter("id");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_UPDATE_SUCC);
		if(StringUtils.isEmpty(id)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_DATA_PK_IS_EMPTY);
			logger.info("报价方案年利率配置,修改数据,参数id为空");
			return result;
		}
		PrjQuatPrpsCfgAiVO entity = null;
		try {
			entity = this.getRequestParamForPrjQuatPrpsCfgAiModify(request);
			entity.setId(Long.valueOf(id));
			boolean success = this.prjQuatPrpsCfgAiService.modify(entity);
			if(!success){
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
				logger.info("报价方案年利率配置,修改数据,业务异常entity:{}", JSON.toJSONString(entity));
			}
			logger.info("报价方案年利率配置,新增数据成功entity:{}", JSON.toJSONString(entity));
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("报价方案年利率配置,新增数据,业务异常msg:{}",ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("报价方案年利率配置,新增数据,系统异常,msg:{}",ex.getMessage());
		}
		return result;
	}
	//**************************************************私有方法************************************************/
	
	/**
	 * 获取请求参数
	 * @param request
	 * @return
	 * @throws BaseException
	 */
	private PrjQuatPrpsCfgAiVO getRequestParamForPrjQuatPrpsCfgAi(HttpServletRequest request) throws BaseException {
		String stNum = request.getParameter("stNum");//开始期数
		String endNum = request.getParameter("endNum");//结束期数
		String lsPdUntCd = request.getParameter("lsPdUntCd");//租赁期数单位
		String annIntRatePct = request.getParameter("annIntRatePct");//年利率
		String isAllowChg = request.getParameter("isAllowChg");//是否允许变更
		String annIntRatePct2 = request.getParameter("annIntRatePct2");//新能源第二张支付表年利率
		
		//参数验证
		if (StringUtils.isEmpty(stNum)||!StringUtils.isNumeric(stNum)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "开始期数不能为空,并且为数字类型!");
		}
		if (StringUtils.isEmpty(endNum)||!StringUtils.isNumeric(endNum)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "结束期数不能为空,并且为数字类型!");
		}
		if (StringUtils.isEmpty(annIntRatePct)||!StringUtils.isUnsignedNumeric(annIntRatePct)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "年利率不能为空,并且为正数!");
		}
		
		PrjQuatPrpsCfgAiVO prjQuatPrpsCfgAiVO = new PrjQuatPrpsCfgAiVO();
		if(StringUtils.isNotEmpty(stNum)){
			prjQuatPrpsCfgAiVO.setStNum(Long.valueOf(stNum));
		}
		if(StringUtils.isNotEmpty(endNum)){
			prjQuatPrpsCfgAiVO.setEndNum(Long.valueOf(endNum));
		}
		prjQuatPrpsCfgAiVO.setLsPdUntCd(lsPdUntCd);
		if(StringUtils.isNotEmpty(annIntRatePct)){
			prjQuatPrpsCfgAiVO.setAnnIntRatePct(Double.valueOf(annIntRatePct));
		}
		if(StringUtils.isNotEmpty(isAllowChg)){
			prjQuatPrpsCfgAiVO.setIsAllowChg(Integer.valueOf(isAllowChg));
		}
		if(StringUtils.isNotEmpty(annIntRatePct2)){
			prjQuatPrpsCfgAiVO.setAnnIntRatePct2(Double.valueOf(annIntRatePct2));
		}
		
		return prjQuatPrpsCfgAiVO;
	}
	
	/**
	 * 获取报价方案请求参数(新增)
	 * @param request 请求
	 * @return 
	 */
	private PrjQuatPrpsCfgAiVO getRequestParamPrjQuatPrpsCfgAiAdd(HttpServletRequest request)throws BaseException{
		String prpsCfgId = request.getParameter("prpsCfgId");//备注
		if (StringUtils.isBlank(prpsCfgId)) {
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_604, "请先维护报价方案基本信息!");
		}
		PrjQuatPrpsCfgAiVO prjQuatPrpsCfgAiVO = this.getRequestParamForPrjQuatPrpsCfgAi(request);
		prjQuatPrpsCfgAiVO.setPrpsCfgId(Long.valueOf(prpsCfgId));
		prjQuatPrpsCfgAiVO.setCrtTm(new Date());
		prjQuatPrpsCfgAiVO.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		return prjQuatPrpsCfgAiVO;
	}
	
	/**
	 * 获取报价方案请求参数(修改)
	 * @param request 请求
	 * @return 
	 */
	private PrjQuatPrpsCfgAiVO getRequestParamForPrjQuatPrpsCfgAiModify(HttpServletRequest request)throws BaseException{
		PrjQuatPrpsCfgAiVO prjQuatPrpsCfgAiVO = this.getRequestParamForPrjQuatPrpsCfgAi(request);
		prjQuatPrpsCfgAiVO.setMdfTm(new Date());
		prjQuatPrpsCfgAiVO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		return prjQuatPrpsCfgAiVO;
	}
}

